﻿<?xml version='1.0'?>
<xsl:stylesheet
    version='2.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
    xmlns:xlink="http://www.w3.org/1999/xlink">
    
<xsl:output method="xml" indent="yes" standalone="no"
              doctype-system="http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"
              doctype-public="-//W3C//DTD SVG 1.1//EN" />

<!-- File name for the fractal transformation -->
<xsl:param name="fractalTransformXml">default</xsl:param>
<!-- Global Variables -->
<xsl:variable name="maxDepthLevel" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
              select="xs:integer(//@maxDepthLevel)"/>  <!-- Need to cast this as integer for use in ranges -->
<xsl:variable name="maxDepthLimit" select="$maxDepthLevel - 1"/>  
  
<!-- Create root node -->
<xsl:template match="/">
  <svg xmlns="http://www.w3.org/2000/svg">
    <xsl:attribute name="viewBox"><xsl:copy-of select="//@viewBox" /></xsl:attribute>
    <defs xmlns="http://www.w3.org/2000/svg">
    
      <!-- Depth 0 -->
  	  <g xmlns="http://www.w3.org/2000/svg">
	      <xsl:attribute name="id">depth_0</xsl:attribute>
	     <xsl:copy-of xmlns:svg="http://www.w3.org/2000/svg" select="//svg:g/*" />      </g>
    		
      <!-- Depths 1 to maxDepthLevel - 1 -->
		  <xsl:for-each select="for $i in (1 to $maxDepthLimit) return $i">
        <g xmlns="http://www.w3.org/2000/svg">
          <xsl:attribute name="id">depth_<xsl:value-of select="." /></xsl:attribute>
          <xsl:apply-templates select="document( $fractalTransformXml )//transform">
            <xsl:with-param name="depthNum" select=". - 1" />
          </xsl:apply-templates>
    	  </g>
      </xsl:for-each>
    		
    </defs>
    
      <!-- Depths to be displayed -->
      <xsl:choose>
        <xsl:when test="//@viewOption = 'all'">
          <!-- Trick I found online to select other nodes from within for-each -->
          <xsl:variable name="root" select="//*"/> 
  	      <xsl:for-each select="for $i in (0 to $maxDepthLimit) return $i">
            <xsl:apply-templates select="$root[@transform]">
              <xsl:with-param name="depthNum" select="." />  
              <xsl:with-param name="opacity" select="(. + 1) div $maxDepthLevel" />
            </xsl:apply-templates>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates xmlns:svg="http://www.w3.org/2000/svg" select="//svg:g" />
        </xsl:otherwise>
      </xsl:choose>    
    
  </svg>
</xsl:template>

<!-- Template for use elements -->
<xsl:template match="*[@transform]" name="useElement">
  <xsl:param name="depthNum"><xsl:value-of select="$maxDepthLimit" /></xsl:param>
  <xsl:param name="opacity">0</xsl:param>

  <use xmlns="http://www.w3.org/2000/svg">
    <xsl:attribute name="xlink:href">#depth_<xsl:value-of select="$depthNum" /></xsl:attribute>
    <xsl:if test="$opacity > 0">
      <xsl:attribute name="opacity">
        <xsl:value-of select="$opacity" />
      </xsl:attribute>    
    </xsl:if>
    <xsl:attribute name="transform">
      <xsl:copy-of select="@transform" />
    </xsl:attribute>
  </use>
</xsl:template>

</xsl:stylesheet>
